<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px" size="small">
      <el-form-item label="项目编码" prop="projectCode">
        <el-input v-model="dataForm.projectCode" placeholder="项目编码"  maxlength="100"></el-input>
      </el-form-item>
    <el-form-item label="项目名称" prop="projectName">
      <el-input v-model="dataForm.projectName" placeholder="项目名称"  maxlength="100"></el-input>
    </el-form-item>
    <el-form-item label="项目领导" prop="projectHead">
      <el-input v-model="dataForm.projectHead" placeholder="项目领导"  maxlength="100"></el-input>
    </el-form-item>
    <el-form-item label="项目联系电话" prop="projectTel">
      <el-input v-model="dataForm.projectTel" placeholder="项目联系电话"  maxlength="20"></el-input>
    </el-form-item>
      <el-form-item label="碳减排目标(tCO2e)">
        <el-input v-model="dataForm.co2Target" placeholder="碳减排目标" type="number" maxlength="20"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false" size="small">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" size="small">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {isPhone,isMobile} from '@/utils/validate'
  export default {
    data () {
      var validatePhone = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('电话不能为空'));
        } else {
          if (!(isPhone(value)||isMobile(value))) {
            callback(new Error('电话格式不对'));
          } else {
            callback();
          }
        }
      };
      return {
        visible: false,
        dataForm: {
          projectId: 0,
          projectCode:'',
          projectName: '',
          projectHead: '',
          projectTel: '',
          co2Target: 0,
          createTime: '',
          customerId: ''
        },
        dataRule: {
          projectName: [
            { required: true, message: '项目名称不能为空', trigger: 'blur' }
          ],
          projectHead: [
            { required: true, message: '项目领导不能为空', trigger: 'blur' }
          ],
          projectTel: [
            { required: true, message: '项目联系电话不能为空', trigger: 'blur' },
            { validator: validatePhone, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.projectId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.projectId) {
            this.$api.ecs_ecsprojectinfo_info({
              projectId: this.dataForm.projectId,
            }).then((data) => {
              if (data && data.code === 0) {
                this.dataForm.projectCode = data.ecsprojectinfo.projectCode
                this.dataForm.projectName = data.ecsprojectinfo.projectName
                this.dataForm.projectHead = data.ecsprojectinfo.projectHead
                this.dataForm.projectTel = data.ecsprojectinfo.projectTel
                this.dataForm.co2Target = data.ecsprojectinfo.co2Target
                this.dataForm.customerId = data.ecsprojectinfo.customerId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$api.ecs_ecsprojectinfo_x({
              url: `/ecs/ecsprojectinfo/${!this.dataForm.projectId ? 'save' : 'update'}`,
              'projectId': this.dataForm.projectId || undefined,
              'projectCode': this.dataForm.projectCode,
              'projectName': this.dataForm.projectName,
              'projectHead': this.dataForm.projectHead,
              'projectTel': this.dataForm.projectTel,
              'co2Target': this.dataForm.co2Target,
              "customerId": this.$util.cookies.get('customer_id'),
            }).then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
